package copyengine.ui.component.panel
{
	import copyengine.ui.component.CEUISprite;
	import copyengine.ui.component.button.CEButton;
	import copyengine.ui.component.scrollbar.CEScrollBarCore;
	import copyengine.ui.component.scrollbar.CEScrollBarCoreEvent;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;

	public final class CEVPanel extends CEUISprite
	{
		/**
		 * turn to next one/page element
		 */
		private var nextOneBtn:CEButton;
		private var nextPageBtn:CEButton;

		/**
		 *turn to prev one/page element.
		 */
		private var prevOneBtn:CEButton;
		private var prevPageBtn:CEButton;

		private var startBtn:CEButton;
		private var endBtn:CEButton;

		/**
		 * Panel
		 */
		private var ceScrollableVPanel:CEScrollableVPanel;

		/**
		 * ScrollBar
		 */
		private var ceScrollBarCore:CEScrollBarCore;

		public function CEVPanel(_target:DisplayObjectContainer,
								 _ceScrollableVPanel:CEScrollableVPanel,
								 _ceScrollBarCore:CEScrollBarCore = null,
								 _nextOneBtn:CEButton = null, _nextPageBtn:CEButton = null,
								 _prevOneBtn:CEButton = null, _prevPageBtn:CEButton = null,
								 _startBtn:CEButton = null, _endBtn:CEButton = null)
		{
			super(_target);
			nextOneBtn = _nextOneBtn;
			nextPageBtn = _nextPageBtn;

			prevOneBtn = _prevOneBtn;
			prevPageBtn = _prevPageBtn;

			startBtn = _startBtn;
			endBtn = _endBtn;

			ceScrollBarCore = _ceScrollBarCore;
			ceScrollableVPanel = _ceScrollableVPanel;

			initialize();
		}

		//========================================//
		//==                           Public Interface                                ==//
		//========================================//

		public function addSubElement(_element:CEPanelSubElement):void
		{
			ceScrollableVPanel.addSubElement(_element);
		}

		//=========================================//
		//==       Initialize&Dispose&AddListner&RemoveLister      ==//
		//=========================================//
		protected function initialize():void
		{
			if (ceScrollBarCore != null)
			{
				ceScrollBarCore.initializeScrollBar(ceScrollableVPanel.getLineScrollSize(),
													ceScrollableVPanel.getPageScrollSize(),
													ceScrollableVPanel.minScrollValue,
													ceScrollableVPanel.maxScrollValue);
			}
			addListener();
		}

		override protected function doDispose():void
		{
			removeListener();

			GeneralUtils.removeTargetFromParent(nextOneBtn);
			GeneralUtils.removeTargetFromParent(nextPageBtn);
			GeneralUtils.removeTargetFromParent(prevOneBtn);
			GeneralUtils.removeTargetFromParent(prevPageBtn);
			GeneralUtils.removeTargetFromParent(startBtn);
			GeneralUtils.removeTargetFromParent(endBtn);
			GeneralUtils.removeTargetFromParent(ceScrollBarCore);
			GeneralUtils.removeTargetFromParent(ceScrollableVPanel);
		}

		private function addListener():void
		{
			GeneralUtils.addTargetEventListener(nextOneBtn, MouseEvent.CLICK, onNextOneBtnClick);
			GeneralUtils.addTargetEventListener(nextPageBtn, MouseEvent.CLICK, onNextPageBtnClick);
			GeneralUtils.addTargetEventListener(prevOneBtn, MouseEvent.CLICK, onPrevOneBtnClick);
			GeneralUtils.addTargetEventListener(prevPageBtn, MouseEvent.CLICK, onPrevPageBtnClick);
			GeneralUtils.addTargetEventListener(startBtn, MouseEvent.CLICK, onStartBtnClick);
			GeneralUtils.addTargetEventListener(endBtn, MouseEvent.CLICK, onEndBtnClick);
			GeneralUtils.addTargetEventListener(ceScrollBarCore, CEScrollBarCoreEvent.SCROLL, onCEScrollBarScroll)
			GeneralUtils.addTargetEventListener(ceScrollableVPanel, MouseEvent.MOUSE_WHEEL, onCEScrollableVPanelMouseWheel);
			GeneralUtils.addTargetEventListener(ceScrollableVPanel, CEScrollableVPanelEvent.SCROLL_START, onCEScrollableVPanelScroll);
			GeneralUtils.addTargetEventListener(ceScrollableVPanel, CEScrollableVPanelEvent.PANEL_SIZE_CHANGE, onVPanelReLayoutCallBack);
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(nextOneBtn, MouseEvent.CLICK, onNextOneBtnClick);
			GeneralUtils.removeTargetEventListener(nextPageBtn, MouseEvent.CLICK, onNextPageBtnClick);
			GeneralUtils.removeTargetEventListener(prevOneBtn, MouseEvent.CLICK, onPrevOneBtnClick);
			GeneralUtils.removeTargetEventListener(prevPageBtn, MouseEvent.CLICK, onPrevPageBtnClick);
			GeneralUtils.removeTargetEventListener(startBtn, MouseEvent.CLICK, onStartBtnClick);
			GeneralUtils.removeTargetEventListener(endBtn, MouseEvent.CLICK, onEndBtnClick);
			GeneralUtils.removeTargetEventListener(ceScrollBarCore, CEScrollBarCoreEvent.SCROLL, onCEScrollBarScroll)
			GeneralUtils.removeTargetEventListener(ceScrollableVPanel, MouseEvent.MOUSE_WHEEL, onCEScrollableVPanelMouseWheel);
			GeneralUtils.removeTargetEventListener(ceScrollBarCore, CEScrollableVPanelEvent.SCROLL_START, onCEScrollableVPanelScroll);
			GeneralUtils.removeTargetEventListener(ceScrollableVPanel, CEScrollableVPanelEvent.PANEL_SIZE_CHANGE, onVPanelReLayoutCallBack);
		}

		private function refreshButtonComponent():void
		{
			var isEnableNextBtn:Boolean = ceScrollableVPanel.scrollPosition == ceScrollableVPanel.maxScrollValue ? false : true;
			var isEnablePrevBtn:Boolean = ceScrollableVPanel.scrollPosition == ceScrollableVPanel.minScrollValue ? false : true;

			GeneralUtils.changeTargetEnableProperty(isEnableNextBtn, nextOneBtn);
			GeneralUtils.changeTargetEnableProperty(isEnableNextBtn, nextPageBtn);
			GeneralUtils.changeTargetEnableProperty(isEnableNextBtn, endBtn);
			GeneralUtils.changeTargetEnableProperty(isEnablePrevBtn, prevOneBtn);
			GeneralUtils.changeTargetEnableProperty(isEnablePrevBtn, prevPageBtn);
			GeneralUtils.changeTargetEnableProperty(isEnablePrevBtn, startBtn);
		}

		/**
		 * 当VPanel的大小发生改变会回调该函数,该函数重新刷新ScrollBar的大小等
		 */
		private function onVPanelReLayoutCallBack(e:CEScrollableVPanelEvent):void
		{
			if (ceScrollBarCore != null)
			{
				ceScrollBarCore.refreshScrollBar(ceScrollableVPanel.getLineScrollSize(),
												 ceScrollableVPanel.getPageScrollSize(),
												 ceScrollableVPanel.minScrollValue,
												 ceScrollableVPanel.maxScrollValue,
												 ceScrollableVPanel.scrollPosition);
			}
			refreshButtonComponent();
		}

		/**
		 *当玩家拖动Scrollbar时候改变VPanel 和onCEScrollableVPanelScroll 成对使用
		 */
		private function onCEScrollBarScroll(e:CEScrollBarCoreEvent):void
		{
			ceScrollableVPanel.scrollPosition = e.scrollPosition;
			refreshButtonComponent();
		}

		/**
		 *当VPanel发生Scroll时候改变ScrollBar 和 onCEScrollBarScroll成对使用
		 */
		private function onCEScrollableVPanelScroll(e:CEScrollableVPanelEvent):void
		{
			ceScrollBarCore.scrollPosition = e.expectScrollPositon;
			refreshButtonComponent();
		}

		//=============================================//
		//==                             Mouse Event Handle                                  ==//
		//=============================================//

		private function onNextOneBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToNextOne();
			refreshButtonComponent();
		}

		private function onNextPageBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToNextPage();
			refreshButtonComponent();
		}

		private function onPrevOneBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToPrevOne();
			refreshButtonComponent();
		}

		private function onPrevPageBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToPrevPage();
			refreshButtonComponent();
		}

		private function onStartBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToStart();
			refreshButtonComponent();
		}

		private function onEndBtnClick(e:MouseEvent):void
		{
			ceScrollableVPanel.scrollToEnd();
			refreshButtonComponent();
		}

		private function onCEScrollableVPanelMouseWheel(e:MouseEvent):void
		{
			ceScrollBarCore && e.delta < 0 ? onNextOneBtnClick(null) : onPrevOneBtnClick(null);
		}

	}
}
